package com.ct.gd.passport.view.travel;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.domain.TravelPortInfo;
import com.ct.gd.passport.view.AbsCatalogFragment;
import com.up91.common.android.content.SimpleLoader;
import com.up91.common.android.view.adapter.SimpleListAdapter;

public class TravelPortInfoFragment extends AbsCatalogFragment<TravelPortInfo>{
	@Override
	protected SimpleListAdapter<TravelPortInfo> onInitListAdapter() {
		return new TravelPortInfoAdapter(getActivity(), null);
	}
	
	private static class TravelPortInfoAdapter extends SimpleListAdapter<TravelPortInfo> {
		private class ViewHolder {
			TextView tvPortName;
			TextView tvFlowOfPeople;
			TextView tvWaitingPeople;
		}

		public TravelPortInfoAdapter(Context context, List<TravelPortInfo> data) {
			super(context, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.passport_app_history_item, null);
				holder = new ViewHolder();
				holder.tvPortName = (TextView) convertView.findViewById(R.id.passport_app_history_item_name);
				holder.tvFlowOfPeople = (TextView) convertView.findViewById(R.id.passport_app_history_item_date);
				holder.tvWaitingPeople = (TextView) convertView.findViewById(R.id.passport_app_history_item_area);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			TravelPortInfo t = getData().get(position);
			holder.tvPortName.setText(t.getPortName());
			holder.tvFlowOfPeople.setText(t.getThrough());
			holder.tvWaitingPeople.setText( t.getWait());
			return convertView;
		}
	}
	
	@Override
	protected void beforeLoad(Loader<List<TravelPortInfo>> loader) {
	}

	@Override
	public Loader<List<TravelPortInfo>> onCreateLoader(int id, Bundle args) {
		return new TravelPortInfoLoader(getActivity(), getListView());
	}
	
	private static class TravelPortInfoLoader extends SimpleLoader<List<TravelPortInfo>> {
		public TravelPortInfoLoader(Context context, View view) {
			super(context, view);
		}
		@Override
		protected List<TravelPortInfo> loadData() throws Exception {
			return TravelPortInfo.load();
		}
	}
}
